﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 结构体03
{
    struct Student
    {
        public int Id;
        public string Name;
        public int Age;
        public bool Sex;
        public double Score;

        public Student(int id, string name, int age, bool sex, double score)
        {
            Id = id;
            Name = name;
            Age = age;
            Sex = sex;
            Score = score;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // 1、创建学生的结构体，包含学号、姓名、年龄、性别、分数，然后创建一个学生，给所有的信息赋值，并打印到控制台上
            Student stu01 = new Student(1001, "stu01", 12, false, 87);
            //stu01.Id = 1001;
            //stu01.Name = "stu01";
            //stu01.Age = 12;
            //stu01.Sex = false;
            //stu01.Score = 87;
            Console.WriteLine("{0}, {1}, {2}, {3}, {4}", stu01.Id, stu01.Name, stu01.Age, stu01.Sex, stu01.Score);

            // 2、有5名学生保存在结构体数组当中，编程求出分出最高的学生姓名，并输出该学生的全部信息
            Student stu02 = new Student(1002, "stu02", 13, true, 65);
            Student stu03 = new Student(1003, "stu03", 11, true, 60);
            Student stu04 = new Student(1004, "stu04", 15, false, 98);
            Student stu05 = new Student(1005, "stu05", 14, true, 78);

            Student[] stus = new Student[5];
            stus[0] = stu01;
            stus[1] = stu02;
            stus[2] = stu03;
            stus[3] = stu04;
            stus[4] = stu05;
            // 假设第一个学生的分数最高
            int indexMaxScoreStu = 0;
            double maxScore = stus[0].Score;
            for (int i = 0; i < stus.Length; i++)
            {
                if (stus[i].Score > maxScore)
                {
                    maxScore = stus[i].Score;
                    indexMaxScoreStu = i;
                }
            }
            Console.WriteLine("最高是{0}， 是{1}考的", maxScore, stus[indexMaxScoreStu].Name);

            // 3、对上述5名学生数组按年龄进行排序，并输出学生的姓名
            for (int i = 0; i < stus.Length - 1; i++)
            {
                for (int j = 0; j < stus.Length - 1 - i; j++)
                {
                    if (stus[j].Age < stus[j + 1].Age)
                    {
                        Student stuTemp = stus[j];
                        stus[j] = stus[j + 1];
                        stus[j + 1] = stuTemp;
                    }
                }
            }

            for (int i = 0; i < stus.Length; i++)
            {
                Console.WriteLine(stus[i].Name);
            }
        }
    }
}
